/**
 * @fileOverview Greasy Chrome - Only for eRepublik Advanced for now.
 * @author frimen
 * @version 1.0.0
 */
 
var greasyChrome={
	healthyKeyPattern:/^(nngProjects|day\d{4}-\d{2}-\d{2}|m\d+|activeMantis)$/i,
	corruptedKeyPattern:/^undefined\.[a-z]+$/i,
	keyIsOurs:function(a){
		if(typeof a!="string")throw new TypeError("Argument not a string.");
		var result = Boolean(this.healthyKeyPattern.test(a)||this.corruptedKeyPattern.test(a))
		return result;
	},
	ourStorageKeys:[],
	getOurStorageKeys:function(){
		if(this.ourStorageKeys.length)this.ourStorageKeys=[];
		for(var a in localStorage) this.keyIsOurs(a)&&this.ourStorageKeys.push(a);
		return this.ourStorageKeys
	},
	ourStoragePairs:{},
	getOurStoragePairs:function(){
		if(this.ourStoragePairs.length)this.ourStoragePairs={};
		for(var a in localStorage)this.keyIsOurs(a)&&(this.ourStoragePairs[a]=localStorage.getItem(a));
		return this.ourStoragePairs
	}
};
function GM_addStyle(a){
	document.getElementsByTagName("head")[0].appendChild(document.createElement("style")).textContent=a
}
function GM_deleteValue(a){
	chrome.extension.sendRequest({action:"deleteFromStorage", key:a});
	return localStorage.removeItem(a)
}

function GM_getValue(a,b){
	return localStorage.getItem(a)===null?b:localStorage.getItem(a)
}

function GM_log(a){
	return console.log(a)
}

function GM_setValue(a,b){
	chrome.extension.sendRequest({action:"setToStorage",key:a,value:b,site:site});
	return localStorage.setItem(a,b)
}

function GM_xmlhttpRequest(a){
	a.method=="GET"&&chrome.extension.sendRequest({action:"xhrGET",url:a.url},
		function(b){
			a.onload(JSON.parse(b.xhrObject))
		}
	)
}

function GM_listValues(){
	return greasyChrome.getOurStorageKeys()
}

chrome.extension.sendRequest(
	{	action:"syncStorageUp",
		data:JSON.stringify(greasyChrome.getOurStoragePairs())
	}, function(){
		chrome.extension.sendRequest({action:"syncStorageDown"},
			function(a){
				for(var b in localStorage)greasyChrome.keyIsOurs(b)&&localStorage.removeItem(b);
				for(b in a=JSON.parse(a))localStorage.setItem(b,a[b]);
				typeof nngHelper=="undefined"?
					greasyChrome._eraMainStarted=!1:(greasyChrome._eraMainStarted=!0,nngHelper())
			}
		)
	}
);

setInterval(function () {
chrome.extension.sendRequest(
	{	action:"getActive"
	}, function(a){
		if (a) {
			localStorage.setItem('activeMantis',a);
			try {
				a = eval('('+a+')');
			} catch (e) {
				console.log('szittya:'+scriptJQ.toJSON(a)+'|'+scriptJQ.toJSON(active)+'|'+e);
			}
			if (!sameTrack(active, a)) {
				mantisHeader();
			}
		}
	}
)
},1000);
